<template>
	<div class="add_edit agreement_add_edit">
		<use-box>
			<el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
				<el-form-item label="名称" prop="name" ref="name">
					<el-input v-model="form.name" placeholder="请输入协议名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="分类" prop="type" ref="type">
					<el-input v-model="form.type" placeholder="请输入分类" clearable></el-input>
				</el-form-item>
				<el-form-item label="内容" prop="content" ref="content">
					<use-editor ref="editor" @editorChange="editorChange"></use-editor>
				</el-form-item>
				<el-form-item label="排序">
					<el-input type="number" v-model.number="form.sort" :min="1" placeholder="请输入排序" clearable
						oninput="value = parseInt(value) || 1">
					</el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="form.state" active-color="#ff6a6c" inactive-color="#bbb" active-value="启用"
						inactive-value="禁用"></el-switch>
				</el-form-item>
				<el-form-item>
					<use-affix position="bottom" :offset="58">
						<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
							@click="submitForm('ruleForm')">提交</el-button>
						<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
					</use-affix>
				</el-form-item>
			</el-form>
		</use-box>
	</div>
</template>

<script>
	const __name = 'wmzmall-sys-agreement';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					name: '',
					type: '',
					content: '',
					sort: 0,
					state: '启用'
				},
				rules: {
					name: [{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}],
					type: [{
						required: true,
						message: '请输入分类',
						trigger: 'change'
					}],
					content: [{
						required: true,
						message: '请输入内容',
						trigger: 'change'
					}]
				}
			};
		},
		methods: {
			async loadData() {
				if (!this.dataId) {
					this.$db[__name]
						.noshopid()
						.noaction()
						.getsort().then(res => {
							if (res.code == 200) {
								this.form.sort = ++res.datas.sort;
							}
						});
					return;
				}

				await this.$db[__name]
					.noshopid()
					.noaction()
					.tofirst(this.dataId).then(res => {
						if (res.code == 200) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}

							if (res.datas && res.datas.content) {
								this.form.content = res.datas.content;
								this.$refs.editor.setContent(this.form.content);
							}
						}
					});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.formLoading = true;
						if (!this.dataId) {
							this.$db[__name]
								.noshopid()
								.noaction()
								.add(this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						} else {
							this.$db[__name]
								.noshopid()
								.noaction()
								.update(this.dataId, this.form).then(res => {
									if (res.code == 200) {
										this.getOpenerEventChannel().emit('loadData');
										this.returnPage();
									}
									this.formLoading = false;
								}).catch(() => {
									this.formLoading = false;
								})
						}
					}
				});
			},
			editorChange(res) {
				this.form.content = res;
				if (res == '') {
					this.$refs.ruleForm.validateField('content');
				} else {
					this.$refs.content.clearValidate();
				}
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/system_set/agreement/agreement'
				});
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.use-box', this.$route.path);
			});

			this.$nextTick(() => {
				this.$api.scrollToStorage('.use-box', this.$route.path);
			});
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style>
	.agreement_add_edit>>>.editor {
		width: 50%;
	}
</style>
